package cn.edu.zafu.bigdata.hdb;
class Cube{
    private double length;
    private double width;
    private double height;
    //带参构造方法
    public Cube(double length, double width, double height){
        this.length=length;
        this.width=width;
        this.height=height;
    }
    //获取长
    public double getLength() {
        return length;
    }
    //获取宽
    public double getWidth() {
        return width;
    }
    //获取高
    public double getHeight() {
        return height;
    }
    //重写equals方法
    @Override
    public boolean equals(Object obj){
        //判断是否为同一对象
        if(this == obj) return true;
        //判断是否是null或者是否属于不同的类
        if(obj == null || getClass() != obj.getClass()) return false;
        //类型转换
        Cube other=(Cube) obj;
        //判断长宽高是否相等
        return Double.compare(this.length, other.length)==0 &&
                Double.compare(this.width,other.width)==0 &&
                Double.compare(this.height,other.height)==0;
    }
    //重写hashCode方法
    @Override
    public int hashCode(){
        int result=Double.hashCode(length);
        result =31 * result + Double.hashCode(width);
        result =31 * result + Double.hashCode(height);
        return result;
    }

}
public class TeatCube {
    public static void main(String[] args){
        //创建几个Cube对象
        Cube cube1=new Cube(3.0,4.0,5.0);
        Cube cube2=new Cube(3.0,4.0,5.0);
        Cube cube3=new Cube(2.0,4.0,5.0);
        Cube cube4=new Cube(3.0,4.0,6.0);
        Cube cube5=new Cube(3.0,4.0,5.0);
        //测试相等性
        System.out.println("cube1.equals(cube2):"+cube1.equals(cube2));
        System.out.println("cube1.equals(cube3):"+cube1.equals(cube3));
        System.out.println("cube1.equals(cube4):"+cube1.equals(cube4));
        System.out.println("cube1.equals(cube5):"+cube1.equals(cube5));
        //测试与null的相等性
        System.out.println("cube1.equals(null):"+cube1.equals(null));
        //测试与不同类型的相等性
        System.out.println("cube1.=equals(\"string\"):"+cube1.equals("string"));
    }
}
